/*
PyLink-SR (Python Linked Software Radio System)
(c) Bob Cowdery (G3UKB) 2008

File: pylink_main.py
Date: December 2008

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

The author can be reached by email at

bob@g3ukb.co.uk

========================================================================
Module Description:
An experimental Web GUI - the client side javascript

*/

//-----------------------------------------------------------------------
var pylink_client = {
   
    init: function() {
        // Grab the elements we'll need.                                                                                                                                      
        pylink_client.power = document.getElementById("b_refresh");
        
        pylink_client.lsb = document.getElementById("b_lsb");
        pylink_client.usb = document.getElementById("b_usb");
        pylink_client.cwl = document.getElementById("b_cwl");
        pylink_client.cwu = document.getElementById("b_cwu");
        pylink_client.am = document.getElementById("b_am");
        pylink_client.nfm = document.getElementById("b_nfm");
        
        pylink_client.f6k0 = document.getElementById("b_6k0");
        pylink_client.f2k4 = document.getElementById("b_2k4");
        pylink_client.f2k1 = document.getElementById("b_2k1");
        pylink_client.f1k0 = document.getElementById("b_1k0");
        pylink_client.f500 = document.getElementById("b_500");
        pylink_client.f250 = document.getElementById("b_250");
        
        pylink_client.mhz100 = document.getElementById("100mhz");
        pylink_client.mhz10 = document.getElementById("10mhz");
        pylink_client.mhz1 = document.getElementById("1mhz");
        pylink_client.khz100 = document.getElementById("100khz");
        pylink_client.khz10 = document.getElementById("10khz");
        pylink_client.khz1 = document.getElementById("1khz");
        pylink_client.hz100 = document.getElementById("100hz");
        pylink_client.hz10 = document.getElementById("10hz");
        pylink_client.hz1 = document.getElementById("1hz");
              
        // Hijack the controls and add event listeners                                                                                                                                                   
        YAHOO.util.Event.addListener(pylink_client.power, "click", pylink_client.frefresh);

        YAHOO.util.Event.addListener(pylink_client.lsb, "click", pylink_client.flsb);
        YAHOO.util.Event.addListener(pylink_client.usb, "click", pylink_client.fusb);
        YAHOO.util.Event.addListener(pylink_client.cwl, "click", pylink_client.fcwl);
        YAHOO.util.Event.addListener(pylink_client.cwu, "click", pylink_client.fcwu);
        YAHOO.util.Event.addListener(pylink_client.am, "click", pylink_client.fam);
        YAHOO.util.Event.addListener(pylink_client.nfm, "click", pylink_client.fnfm);
        
        YAHOO.util.Event.addListener(pylink_client.f6k0, "click", pylink_client.ff6k0);
        YAHOO.util.Event.addListener(pylink_client.f2k4, "click", pylink_client.ff2k4);
        YAHOO.util.Event.addListener(pylink_client.f2k1, "click", pylink_client.ff2k1);
        YAHOO.util.Event.addListener(pylink_client.f1k0, "click", pylink_client.ff1k0);
        YAHOO.util.Event.addListener(pylink_client.f500, "click", pylink_client.ff500);
        YAHOO.util.Event.addListener(pylink_client.f250, "click", pylink_client.ff250);
        
        YAHOO.util.Event.addListener(pylink_client.mhz100, "DOMMouseScroll", pylink_client.fmhz100);
        YAHOO.util.Event.addListener(pylink_client.mhz10, "DOMMouseScroll", pylink_client.fmhz10);
        YAHOO.util.Event.addListener(pylink_client.mhz1, "DOMMouseScroll", pylink_client.fmhz1);
        YAHOO.util.Event.addListener(pylink_client.khz100, "DOMMouseScroll", pylink_client.fkhz100);
        YAHOO.util.Event.addListener(pylink_client.khz10, "DOMMouseScroll", pylink_client.fkhz10);
        YAHOO.util.Event.addListener(pylink_client.khz1, "DOMMouseScroll", pylink_client.fkhz1);
        YAHOO.util.Event.addListener(pylink_client.hz100, "DOMMouseScroll", pylink_client.fhz100);
        YAHOO.util.Event.addListener(pylink_client.hz10, "DOMMouseScroll", pylink_client.fhz10);
        YAHOO.util.Event.addListener(pylink_client.hz1, "DOMMouseScroll", pylink_client.fhz1);
        
        YAHOO.util.Event.addListener(pylink_client.mhz100, "mouseover", pylink_client.fmhz100_over);
        YAHOO.util.Event.addListener(pylink_client.mhz100, "mouseout", pylink_client.fmhz100_out);
        YAHOO.util.Event.addListener(pylink_client.mhz10, "mouseover", pylink_client.fmhz10_over);
        YAHOO.util.Event.addListener(pylink_client.mhz10, "mouseout", pylink_client.fmhz10_out);
        YAHOO.util.Event.addListener(pylink_client.mhz1, "mouseover", pylink_client.fmhz1_over);
        YAHOO.util.Event.addListener(pylink_client.mhz1, "mouseout", pylink_client.fmhz1_out);
        YAHOO.util.Event.addListener(pylink_client.khz100, "mouseover", pylink_client.fkhz100_over);
        YAHOO.util.Event.addListener(pylink_client.khz100, "mouseout", pylink_client.fkhz100_out);
        YAHOO.util.Event.addListener(pylink_client.khz10, "mouseover", pylink_client.fkhz10_over);
        YAHOO.util.Event.addListener(pylink_client.khz10, "mouseout", pylink_client.fkhz10_out);
        YAHOO.util.Event.addListener(pylink_client.khz1, "mouseover", pylink_client.fkhz1_over);
        YAHOO.util.Event.addListener(pylink_client.khz1, "mouseout", pylink_client.fkhz1_out);
        YAHOO.util.Event.addListener(pylink_client.hz100, "mouseover", pylink_client.fhz100_over);
        YAHOO.util.Event.addListener(pylink_client.hz100, "mouseout", pylink_client.fhz100_out);
        YAHOO.util.Event.addListener(pylink_client.hz10, "mouseover", pylink_client.fhz10_over);
        YAHOO.util.Event.addListener(pylink_client.hz10, "mouseout", pylink_client.fhz10_out);
        YAHOO.util.Event.addListener(pylink_client.hz1, "mouseover", pylink_client.fhz1_over);
        YAHOO.util.Event.addListener(pylink_client.hz1, "mouseout", pylink_client.fhz1_out);
        
        // Set up the poll timer (100ms for now)
        setInterval(pylink_client.ajax_poll, 100)
        
        // Change the cursor over the digits to look better
        pylink_client.mhz100.style.cursor = 'default';
        pylink_client.mhz10.style.cursor = 'default';
        pylink_client.mhz1.style.cursor = 'default';
        pylink_client.khz100.style.cursor = 'default';
        pylink_client.khz10.style.cursor = 'default';
        pylink_client.khz1.style.cursor = 'default';
        pylink_client.hz100.style.cursor = 'default';
        pylink_client.hz10.style.cursor = 'default';
        pylink_client.hz1.style.cursor = 'default';
    },
    
    // The event handlers
    frefresh: function(e) {
        var cObj = YAHOO.util.Connect.asyncRequest("POST", "/sdr/power/?xhr", pylink_client.ajax_callback, "cmd=refresh");
    },
    
    flsb: function(e) {
        var cObj = YAHOO.util.Connect.asyncRequest("POST", "/sdr/power/?xhr", pylink_client.ajax_callback, "cmd=lsb");
    },
    fusb: function(e) {
        var cObj = YAHOO.util.Connect.asyncRequest("POST", "/sdr/power/?xhr", pylink_client.ajax_callback, "cmd=usb");
    },
    fcwl: function(e) {
        var cObj = YAHOO.util.Connect.asyncRequest("POST", "/sdr/power/?xhr", pylink_client.ajax_callback, "cmd=cwl");
    },
    fcwu: function(e) {
        var cObj = YAHOO.util.Connect.asyncRequest("POST", "/sdr/power/?xhr", pylink_client.ajax_callback, "cmd=cwu");
    },
    fam: function(e) {
        var cObj = YAHOO.util.Connect.asyncRequest("POST", "/sdr/power/?xhr", pylink_client.ajax_callback, "cmd=am");
    },
    fnfm: function(e) {
        var cObj = YAHOO.util.Connect.asyncRequest("POST", "/sdr/power/?xhr", pylink_client.ajax_callback, "cmd=nfm");
    },
    
    ff6k0: function(e) {
        var cObj = YAHOO.util.Connect.asyncRequest("POST", "/sdr/power/?xhr", pylink_client.ajax_callback, "cmd=6k0");
    },
    ff2k4: function(e) {
        var cObj = YAHOO.util.Connect.asyncRequest("POST", "/sdr/power/?xhr", pylink_client.ajax_callback, "cmd=2k4");
    },
    ff2k1: function(e) {
        var cObj = YAHOO.util.Connect.asyncRequest("POST", "/sdr/power/?xhr", pylink_client.ajax_callback, "cmd=2k1");
    },
    ff1k0: function(e) {
        var cObj = YAHOO.util.Connect.asyncRequest("POST", "/sdr/power/?xhr", pylink_client.ajax_callback, "cmd=1k0");
    },
    ff500: function(e) {
        var cObj = YAHOO.util.Connect.asyncRequest("POST", "/sdr/power/?xhr", pylink_client.ajax_callback, "cmd=500");
    },
    ff250: function(e) {
        var cObj = YAHOO.util.Connect.asyncRequest("POST", "/sdr/power/?xhr", pylink_client.ajax_callback, "cmd=250");
    },
    
    fmhz100: function(e) {
        delta = e.detail/3;
        if (delta < 0) {
            cmd = "cmd=mhz100"
        }
        else {
            cmd = "cmd=-mhz100"
        }
        var cObj = YAHOO.util.Connect.asyncRequest("POST", "/sdr/power/?xhr", pylink_client.ajax_callback, cmd);
    },
    fmhz100_over: function(e) {
        YAHOO.util.Dom.setStyle(pylink_client.mhz100,  'opacity', 0.5);
    },    
    fmhz100_out: function(e) {
        YAHOO.util.Dom.setStyle(pylink_client.mhz100,  'opacity', 1.0);
    },
    fmhz10: function(e) {
        delta = e.detail/3;
        if (delta < 0) {
            cmd = "cmd=mhz10"
        }
        else {
            cmd = "cmd=-mhz10"
        }
        var cObj = YAHOO.util.Connect.asyncRequest("POST", "/sdr/power/?xhr", pylink_client.ajax_callback, cmd);
    },
    fmhz10_over: function(e) {
        YAHOO.util.Dom.setStyle(pylink_client.mhz10,  'opacity', 0.5);
    },   
    fmhz10_out: function(e) {
        YAHOO.util.Dom.setStyle(pylink_client.mhz10,  'opacity', 1.0);
    },
    fmhz1: function(e) {
        delta = e.detail/3;
        if (delta < 0) {
            cmd = "cmd=mhz1"
        }
        else {
            cmd = "cmd=-mhz1"
        }
        var cObj = YAHOO.util.Connect.asyncRequest("POST", "/sdr/power/?xhr", pylink_client.ajax_callback, cmd);
    },
    fmhz1_over: function(e) {
        YAHOO.util.Dom.setStyle(pylink_client.mhz1,  'opacity', 0.5);
    },    
    fmhz1_out: function(e) {
        YAHOO.util.Dom.setStyle(pylink_client.mhz1,  'opacity', 1.0);
    },
    fkhz100: function(e) {
        delta = e.detail/3;
        if (delta < 0) {
            cmd = "cmd=khz100"
        }
        else {
            cmd = "cmd=-khz100"
        }
        var cObj = YAHOO.util.Connect.asyncRequest("POST", "/sdr/power/?xhr", pylink_client.ajax_callback, cmd);
    },
    fkhz100_over: function(e) {
        YAHOO.util.Dom.setStyle(pylink_client.khz100,  'opacity', 0.5);
    },    
    fkhz100_out: function(e) {
        YAHOO.util.Dom.setStyle(pylink_client.khz100,  'opacity', 1.0);
    },
    fkhz10: function(e) {
        delta = e.detail/3;
        if (delta < 0) {
            cmd = "cmd=khz10"
        }
        else {
            cmd = "cmd=-khz10"
        }
        var cObj = YAHOO.util.Connect.asyncRequest("POST", "/sdr/power/?xhr", pylink_client.ajax_callback, cmd);
    },
    fkhz10_over: function(e) {
        YAHOO.util.Dom.setStyle(pylink_client.khz10,  'opacity', 0.5);
    },    
    fkhz10_out: function(e) {
        YAHOO.util.Dom.setStyle(pylink_client.khz10,  'opacity', 1.0);
    },
    fkhz1: function(e) {
        delta = e.detail/3;
        if (delta < 0) {
            cmd = "cmd=khz1"
        }
        else {
            cmd = "cmd=-khz1"
        }
        var cObj = YAHOO.util.Connect.asyncRequest("POST", "/sdr/power/?xhr", pylink_client.ajax_callback, cmd);
    },
    fkhz1_over: function(e) {
        YAHOO.util.Dom.setStyle(pylink_client.khz1,  'opacity', 0.5);
    },    
    fkhz1_out: function(e) {
        YAHOO.util.Dom.setStyle(pylink_client.khz1,  'opacity', 1.0);
    },
    fhz100: function(e) {
        delta = e.detail/3;
        if (delta < 0) {
            cmd = "cmd=hz100"
        }
        else {
            cmd = "cmd=-hz100"
        }
        var cObj = YAHOO.util.Connect.asyncRequest("POST", "/sdr/power/?xhr", pylink_client.ajax_callback, cmd);
    },
    fhz100_over: function(e) {
        YAHOO.util.Dom.setStyle(pylink_client.hz100,  'opacity', 0.5);
    },    
    fhz100_out: function(e) {
        YAHOO.util.Dom.setStyle(pylink_client.hz100,  'opacity', 1.0);
    },
    fhz10: function(e) {
        delta = e.detail/3;
        if (delta < 0) {
            cmd = "cmd=hz10"
        }
        else {
            cmd = "cmd=-hz10"
        }
        var cObj = YAHOO.util.Connect.asyncRequest("POST", "/sdr/power/?xhr", pylink_client.ajax_callback, cmd);
    },
    fhz10_over: function(e) {
        YAHOO.util.Dom.setStyle(pylink_client.hz10,  'opacity', 0.5); 
    },    
    fhz10_out: function(e) {
        YAHOO.util.Dom.setStyle(pylink_client.hz10,  'opacity', 1.0);
    },
    fhz1: function(e) {
        delta = e.detail/3;
        if (delta < 0) {
            cmd = "cmd=hz1"
        }
        else {
            cmd = "cmd=-hz1"
        }
        var cObj = YAHOO.util.Connect.asyncRequest("POST", "/sdr/power/?xhr", pylink_client.ajax_callback, cmd);
    },
    fhz1_over: function(e) {
        YAHOO.util.Dom.setStyle(pylink_client.hz1,  'opacity', 0.5);
    },    
    fhz1_out: function(e) {
        YAHOO.util.Dom.setStyle(pylink_client.hz1,  'opacity', 1.0);
    },
    
    //-----------------------------------------------------------------------
    // The main callback for requests
    ajax_callback: {
        success: function(o) {
            // This turns the JSON string into a JavaScript object.
            var response_obj = eval("(" + o.responseText + ")");
            pylink_client.decode_response(response_obj);
        },
        
        failure: function(o) { 
            alert('An error has occurred');
        }
    },
    
    //-----------------------------------------------------------------------
    // Called every 100ms
    ajax_poll: function(e) {
        var cObj = YAHOO.util.Connect.asyncRequest("POST", "/sdr/poll/?xhr", pylink_client.ajax_poll_response, "cmd=null");
    },
    
    //-----------------------------------------------------------------------
    // The callback for the poll
    ajax_poll_response: {
        success: function(o) {
            // This turns the JSON string into a JavaScript object.
            var response_obj = eval("(" + o.responseText + ")");
            pylink_client.decode_response(response_obj);
        },
        
        failure: function(o) { 
            //alert('A poll error has occurred');
        }    
    },
    
    //-----------------------------------------------------------------------
    // These are the decoders and updaters of the display
    //----------------------------------------------------------------------- 
    decode_response: function(response_obj) {
        if(response_obj.freq) {
            freq = new String(response_obj.freq)
            pylink_client.update_display(freq);
        }
        else if (response_obj.mode) {
            pylink_client.update_mode(response_obj.mode);
        }
        else if (response_obj.filter) {
            pylink_client.update_filter(response_obj.filter);
        }
    },
    
    //-----------------------------------------------------------------------
    update_display: function(freq) {
        // Update the complete display with the new frequency
        if (freq.length != 0) {
            pylink_client.mhz100.innerHTML = freq.charAt(0)
            pylink_client.mhz10.innerHTML = freq.charAt(1)
            pylink_client.mhz1.innerHTML = freq.charAt(2)
            pylink_client.khz100.innerHTML = freq.charAt(4)
            pylink_client.khz10.innerHTML = freq.charAt(5)
            pylink_client.khz1.innerHTML = freq.charAt(6)
            pylink_client.hz100.innerHTML = freq.charAt(8)
            pylink_client.hz10.innerHTML = freq.charAt(9)
            pylink_client.hz1.innerHTML = freq.charAt(10)
        }
    },
    
    //-----------------------------------------------------------------------
    update_mode: function(mode) {
        // All this does is change the opacity of the selected button
        // Ok my javascript sucks
        YAHOO.util.Dom.setStyle(pylink_client.lsb,  'opacity', 0.5);
        YAHOO.util.Dom.setStyle(pylink_client.usb,  'opacity', 0.5);
        YAHOO.util.Dom.setStyle(pylink_client.cwl,  'opacity', 0.5);
        YAHOO.util.Dom.setStyle(pylink_client.cwu,  'opacity', 0.5);
        YAHOO.util.Dom.setStyle(pylink_client.am,  'opacity', 0.5);
        YAHOO.util.Dom.setStyle(pylink_client.nfm,  'opacity', 0.5);
        switch(mode) {            
            case 'LSB':
                YAHOO.util.Dom.setStyle(pylink_client.lsb,  'opacity', 1.0);
                break;
            case 'USB':
                YAHOO.util.Dom.setStyle(pylink_client.usb,  'opacity', 1.0);
                break;
            case 'CW-L':
                YAHOO.util.Dom.setStyle(pylink_client.cwl,  'opacity', 1.0);
                break;
            case 'CW-U':
                YAHOO.util.Dom.setStyle(pylink_client.cwu,  'opacity', 1.0);
                break;
            case 'AM':
                YAHOO.util.Dom.setStyle(pylink_client.am,  'opacity', 1.0);
                break;
            case 'NFM':
                YAHOO.util.Dom.setStyle(pylink_client.nfm,  'opacity', 1.0);
                break;
        }
    },
    
    //-----------------------------------------------------------------------
    update_filter: function(filter) {
        // All this does is change the opacity of the selected button
        // Ok my javascript sucks
        YAHOO.util.Dom.setStyle(pylink_client.f6k0,  'opacity', 0.5);
        YAHOO.util.Dom.setStyle(pylink_client.f2k4,  'opacity', 0.5);
        YAHOO.util.Dom.setStyle(pylink_client.f2k1,  'opacity', 0.5);
        YAHOO.util.Dom.setStyle(pylink_client.f1k0,  'opacity', 0.5);
        YAHOO.util.Dom.setStyle(pylink_client.f500,  'opacity', 0.5);
        YAHOO.util.Dom.setStyle(pylink_client.f250,  'opacity', 0.5);
        switch(filter) {
            case '6K0':
                YAHOO.util.Dom.setStyle(pylink_client.f6k0,  'opacity', 1.0);
                break;
            case '2K4':
                YAHOO.util.Dom.setStyle(pylink_client.f2k4,  'opacity', 1.0);
                break;
            case '2K1':
                YAHOO.util.Dom.setStyle(pylink_client.f2k1,  'opacity', 1.0);
                break;
            case '1K0':
                YAHOO.util.Dom.setStyle(pylink_client.f1k0,  'opacity', 1.0);
                break;
            case '500':
                YAHOO.util.Dom.setStyle(pylink_client.f500, 'opacity', 1.0);
                break;
            case '250':
                YAHOO.util.Dom.setStyle(pylink_client.f250,  'opacity', 1.0);
                break;
        }
    }   
};
    
YAHOO.util.Event.addListener(window, "load", pylink_client.init);
